package pt.isec.gpsgroup5.moneybox;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ListObjective extends Activity {
	
	private DBAdapter db;
	private ArrayList<HashMap<String,String>> list;
	private ListView lv;
	//private ListAdapter lAdapter;

	//Called when the activity is first created
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_objective);
		db = new DBAdapter(this);
		db.open(this);
		
		list = new ArrayList<HashMap<String,String>>();
		//loadList();
		
		
		lv =(ListView)findViewById(R.id.list);
		
		SimpleAdapter sa=new SimpleAdapter(this, loadList(), R.layout.list_obj_item,
				new String[] {DbHelper.NAME,"percent",DbHelper.CURRENT_AMOUNT, DbHelper.FINAL_AMOUNT, "deadline", "priority"},
				new int[] {R.id.objName,R.id.objPercent,R.id.objCurrentAmount, R.id.objFinalAmount, R.id.objDeadline, R.id.objPriority});
		//lAdapter = new ListAdapter(this, list);
		lv.setAdapter(sa);
		
		
	     // Click event for single list row
     /*   lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
			
				
			}
        });*/
		
		if(db!=null){
			db.close();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_list_objective, menu);
		return true;
	}
	
	private ArrayList<HashMap<String, String>> loadList(){
		
		ArrayList<HashMap<String,String>> objMap = new ArrayList<HashMap<String,String>> ();
		HashMap<String,String> tmpObj;
		Objective obj = null;
		int percent = -1;

		Log.i("teste", "num obj: " + db.numObjectives());
		for(int i = 0; i < db.numObjectives(); i++){
			
			obj = db.getObjectivePos(i);
			tmpObj = new HashMap<String,String> ();
			tmpObj.put(DbHelper.NAME, obj.getName());
			percent = (int) ((100 * obj.getCurrentAmount()) / obj.getFinalAmount());
			tmpObj.put("percent", "" + percent + "%");
			tmpObj.put(DbHelper.CURRENT_AMOUNT, "" + obj.getCurrentAmount());
			tmpObj.put(DbHelper.FINAL_AMOUNT, "" + obj.getFinalAmount());
			String deadline = obj.getDay() + "-" + obj.getMonth() + "-" + obj.getYear();
			tmpObj.put("deadline", "" + deadline);
			tmpObj.put("priority", "" + obj.getPriority());
			objMap.add(tmpObj);
			Log.i("teste", "objinseridos: " + i);
		}
		
		return objMap;
	}
}


/*
 class ListAdapter extends BaseAdapter{
 

	private Activity act;
	private ArrayList<HashMap<String, String>> list;
	private static LayoutInflater inflater=null;
	
	public ListAdapter(Activity act, ArrayList<HashMap<String, String>> list) {
		this.act = act;
		this.list = list;
		inflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int pos, View view, ViewGroup vg) {
		View v = view;
		if(v==null){
			v = inflater.inflate(R.layout.list_row, null);
		}
		
        TextView name = (TextView)v.findViewById(R.id.objName);
        TextView percent = (TextView)v.findViewById(R.id.objPercent);
        TextView currentAmount = (TextView)v.findViewById(R.id.objCurrentAmount);
        TextView finalAmount = (TextView)v.findViewById(R.id.objFinalAmount);
        TextView deadline = (TextView)v.findViewById(R.id.objDeadline);
        
        HashMap<String, String> obj = new HashMap<String, String>();
        obj = list.get(pos);
        
        name.setText(obj.get(DbHelper.NAME));
        percent.setText(obj.get("percent"));
        currentAmount.setText(obj.get(DbHelper.CURRENT_AMOUNT));
        finalAmount.setText(obj.get(DbHelper.FINAL_AMOUNT));
        deadline.setText(obj.get("deadline"));
        
        
		return v;
	}
	
}
*/